package com.ruoyi.sale.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.base.domain.BaseLottery;
import com.ruoyi.base.domain.vo.LotteryItemVo;
import com.ruoyi.base.domain.vo.LotteryVo;
import com.ruoyi.base.service.IBaseLotteryService;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.sale.mapper.SaleLotteryOneMapper;
import com.ruoyi.sale.domain.SaleLotteryOne;
import com.ruoyi.sale.service.ISaleLotteryOneService;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 直选彩票售卖Service业务层处理
 *
 * @author szm
 * @date 2025-09-23
 */
@Service
public class SaleLotteryOneServiceImpl extends ServiceImpl<SaleLotteryOneMapper,SaleLotteryOne> implements ISaleLotteryOneService {

    @Autowired
    private IBaseLotteryService baseLotteryService;
    @Autowired
    private RedisCache redisCache;

    @Override
    public List<LotteryVo> buyNumLottery(LotteryVo lotteryVo) {

        List<LotteryVo> voList = new ArrayList<>();
        if (lotteryVo != null){
            List<BaseLottery> updateList = new ArrayList<>();
            List<SaleLotteryOne> entityList = new ArrayList<>();
            String baseId = IdUtils.snowflakeId();
            String[] split = lotteryVo.getLotteryNo().split(",");
            for (String str : split){
                SaleLotteryOne entity = new SaleLotteryOne();
                entity.setLotteryNo(str);
                entity.setLotteryDate(new Date());
                entity.setMultiple(lotteryVo.getMultiple());
                entity.setUserName(lotteryVo.getBuyName());
                entity.setUserPhone(lotteryVo.getBuyPhone());
                entity.setWeixinName(lotteryVo.getBuyWeiName());
                entity.setWeixinNo(lotteryVo.getBuyweiNo());
                entity.setLotteryType(1);
                entity.setLotteryName("直选");
                entity.setBaseBuyNo(str);
                entity.setMultiple(lotteryVo.getMultiple());
                entity.setRemark(lotteryVo.getLotteryNo());
                entity.setBaseId(baseId);
                entity.setCreateTime(new Date());
                entity.setUpdateTime(new Date());
                entity.setCreateBy(SecurityUtils.getUsername());
                entity.setUpdateBy(SecurityUtils.getUsername());
                entity.setBuyGroup(lotteryVo.getBuyGroup());
                entityList.add(entity);
                BaseLottery updateVo = baseLotteryService.isMaxBuyNo(lotteryVo.getMultiple(),str,1);
                if (updateVo.getStatus() == 2){
                    baseLotteryService.delRedis();
                    throw new ArithmeticException("直选，彩票号：【"+str+"】购买次数大于最大设置次数，请重新输入!");
                } else {
                    updateList.add(updateVo);
                }
            }
            lotteryVo.setLotteryType(1);
            lotteryVo.setRemark(lotteryVo.getLotteryNo());
            voList.add(lotteryVo);

            //baseLotteryService.updateBatchById(updateList);

            redisCache.setCacheList("oneUpBuy",updateList);
            redisCache.setCacheList("oneData",entityList);

            //this.saveBatch(entityList);
        }
        return voList;
    }

    @Override
    public List<LotteryVo> buyNumLotterySet(LotteryVo lotteryVo) {

        List<LotteryVo> voList = new ArrayList<>();
        if (lotteryVo != null) {
            List<BaseLottery> updateList = new ArrayList<>();
            List<SaleLotteryOne> entityList = new ArrayList<>();
            Map<String, Set<String>> setMap = lotteryVo.getSetMap();
            String baseId = IdUtils.snowflakeId();
            for (String key : setMap.keySet()) {
                Set<String> set = setMap.get(key);
                String keyph = set.stream().collect(Collectors.joining(","));
                for (String str : set) {
                    SaleLotteryOne entity = new SaleLotteryOne();
                    entity.setLotteryNo(str);
                    entity.setLotteryDate(new Date());
                    entity.setMultiple(lotteryVo.getMultiple());
                    entity.setUserName(lotteryVo.getBuyName());
                    entity.setUserPhone(lotteryVo.getBuyPhone());
                    entity.setWeixinName(lotteryVo.getBuyWeiName());
                    entity.setWeixinNo(lotteryVo.getBuyweiNo());
                    entity.setLotteryType(1);
                    entity.setLotteryName("直选");
                    if (key.equals("lott")) {
                        entity.setBaseBuyNo(str);
                    } else {
                        entity.setBaseBuyNo(key);
                    }
                    entity.setMultiple(lotteryVo.getMultiple());
                    entity.setRemark(keyph);
                    entity.setBaseId(baseId);
                    entity.setCreateTime(new Date());
                    entity.setUpdateTime(new Date());
                    entity.setCreateBy(SecurityUtils.getUsername());
                    entity.setUpdateBy(SecurityUtils.getUsername());
                    entity.setBuyGroup(lotteryVo.getBuyGroup());
                    entityList.add(entity);
                    BaseLottery updateVo = baseLotteryService.isMaxBuyNo(lotteryVo.getMultiple(), str, 1);
                    if (updateVo.getStatus() == 2){
                        throw new ArithmeticException("直选，彩票号：【"+str+"】购买次数大于最大设置次数，请重新输入!");
                    } else {
                        updateList.add(updateVo);
                    }
                }
                LotteryVo vo = new LotteryVo();
                vo.setRefNo(key);
                vo.setRemark(keyph);
                vo.setLotteryType(1);
                voList.add(vo);
            }
            baseLotteryService.updateBatchById(updateList);
            this.saveBatch(entityList);
        }
        return voList;
    }

    @Override
    public List<LotteryVo> getImageList(String baseId) {
        List<SaleLotteryOne> imageList = baseMapper.getImageList(baseId);
        List<LotteryVo> voList = imageList.stream().map(x -> {
            LotteryVo one = new LotteryVo();
            one.setRemark(x.getRemark());
            one.setRefNo(x.getBaseBuyNo());
            return one;
        }).collect(Collectors.toList());
        return voList;
    }

    @Override
    public List<LotteryVo> buyNum3DSingle(LotteryVo lotteryVo) {
        List<LotteryVo> voList = new ArrayList<>();
        List<BaseLottery> updateList = new ArrayList<>();
        List<SaleLotteryOne> entityList = new ArrayList<>();
        List<LotteryItemVo> itemList = lotteryVo.getItemList();
        String baseId = IdUtils.snowflakeId();
        for (LotteryItemVo item : itemList){
            String sort = item.getItemVal();
            item.setItemVal(sort);

            SaleLotteryOne entity = new SaleLotteryOne();
            entity.setLotteryNo(sort);
            entity.setLotteryDate(new Date());
            entity.setUserName(lotteryVo.getBuyName());
            entity.setUserPhone(lotteryVo.getBuyPhone());
            entity.setWeixinName(lotteryVo.getBuyWeiName());
            entity.setWeixinNo(lotteryVo.getBuyweiNo());
            entity.setLotteryType(1);
            entity.setLotteryName("直选 3D单式");
            entity.setBaseBuyNo(sort);
            entity.setMultiple(item.getItemMult());
            entity.setRemark(sort);
            entity.setBaseId(baseId);
            entity.setCreateTime(new Date());
            entity.setUpdateTime(new Date());
            entity.setCreateBy(SecurityUtils.getUsername());
            entity.setUpdateBy(SecurityUtils.getUsername());
            entity.setBuyGroup(lotteryVo.getBuyGroup());
            entityList.add(entity);
            BaseLottery updateVo = baseLotteryService.isMaxBuyNo(item.getItemMult(), sort, 1);
            if (updateVo.getStatus() == 2){
                throw new ArithmeticException("直选，彩票号：【"+sort+"】购买次数大于最大设置次数，请重新输入!");
            } else {
                updateList.add(updateVo);
            }
            LotteryVo vo = new LotteryVo();
            vo.setRefNo(sort);
            vo.setRemark(sort);
            vo.setLotteryType(1);
            voList.add(vo);
        }

        redisCache.setCacheList("oneUpBuy",updateList);
        redisCache.setCacheList("oneData",entityList);

        return voList;
    }

    @Override
    public int deleteAll() {
        return baseMapper.deleteAll();
    }


}
